#include "R52DRender.h"

R5_2D_Render::R5_2D_Render():
m_pD3DDevice(NULL),
m_pVertDecl(NULL)
{

}

R5_2D_Render::~R5_2D_Render()
{

}

void R5_2D_Render::Initialise(IDirect3DDevice9 *pD3DDevice)
{
	m_pD3DDevice = pD3DDevice;
	HRESULT Result = pD3DDevice->CreateVertexDeclaration(SCREEN_ELEMENT, &m_pVertDecl);
	if(Result)
	{

	}
}

void R5_2D_Render::PreFlush()
{

	m_pD3DDevice->SetRenderState(D3DRS_FOGENABLE, FALSE);
	m_pD3DDevice->SetRenderState ( D3DRS_ZENABLE, FALSE );
	m_pD3DDevice->SetRenderState ( D3DRS_ZWRITEENABLE, FALSE );
	//m_pD3DDevice->SetRenderState ( D3DRS_SHADEMODE, D3DSHADE_GOURAUD );
	m_pD3DDevice->SetRenderState ( D3DRS_CULLMODE, D3DCULL_CCW );
	m_pD3DDevice->SetRenderState(D3DRS_LIGHTING,FALSE);
	m_pD3DDevice->SetRenderState ( D3DRS_ALPHABLENDENABLE, TRUE );
	m_pD3DDevice->SetRenderState ( D3DRS_SRCBLEND, D3DBLEND_SRCALPHA );
	m_pD3DDevice->SetRenderState ( D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA );;
	m_pD3DDevice->SetTextureStageState ( 0, D3DTSS_COLORARG1, D3DTA_DIFFUSE );
	m_pD3DDevice->SetTextureStageState ( 0, D3DTSS_COLOROP, D3DTOP_SELECTARG1 );
	m_pD3DDevice->SetTextureStageState(1,D3DTSS_COLOROP,D3DTOP_DISABLE);
	m_pD3DDevice->SetTextureStageState(1,D3DTSS_ALPHAOP,D3DTOP_DISABLE);

	//LPDIRECT3DTEXTURE9 pTexture;

	//D3DXCreateTextureFromFile( m_pD3DDevice, L"Media\\earth.jpg", &pTexture);


	
}


void R5_2D_Render::DrawRect(const Pos_2D& tl,const Pos_2D& tr,const Pos_2D& bl,const Pos_2D& br,D3DCOLOR color)
{

	PreFlush();
	static Vertex_2D s_vec[4];
	s_vec[0].x = tl.x;
	s_vec[0].y = tl.y;
	s_vec[0].z = 0;
	s_vec[0].rhw = 1;
	s_vec[0].color = color;

	s_vec[1].x = tr.x;
	s_vec[1].y = tr.y;
	s_vec[1].z = 0;
	s_vec[1].rhw = 1;
	s_vec[1].color = color;

	s_vec[2].x = br.x;
	s_vec[2].y = br.y;
	s_vec[2].z = 0;
	s_vec[2].rhw = 1;
	s_vec[2].color = color;

	s_vec[3].x = bl.x;
	s_vec[3].y = bl.y;
	s_vec[3].z = 0;
	s_vec[3].rhw = 1;
	s_vec[3].color = color;

	m_pD3DDevice->SetVertexDeclaration(m_pVertDecl);
	m_pD3DDevice->DrawPrimitiveUP ( D3DPT_TRIANGLESTRIP,
		2,
		s_vec,
		sizeof ( Vertex_2D ) );
}
